package funtastic.spellingbee;

import java.util.List;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class PracticeActivity extends Activity
{
    private int                   mCurrentIndex;

    private final int             mDisplayCount      = 20;
    public OnItemClickListener    mItemClickListener = new OnItemClickListener()
                                                     {
                                                         @Override
                                                         public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                                                         {
                                                             mSpellingBee.getTextToSpeech().speak(((TextView) view).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
                                                         }
                                                     };
    private ListView              mListView;
    private SpellingBee           mSpellingBee;

    private List<SpellingDetails> mSpellingList;

    public void displayNextSet(View view)
    {
        if ((mCurrentIndex + mDisplayCount) < mSpellingBee.getSpellingArrayList().size())
        {
            mCurrentIndex += 20;
            mSpellingList = mSpellingBee.getSpellingArrayList(mCurrentIndex, mCurrentIndex + mDisplayCount);
            setTitle(getTitleCurrent());
            mListView.setAdapter(new PracticeAdapter(this, R.layout.list_item_practice, mSpellingList));
            mListView.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    mListView.setSelection(0);
                }
            }, 100L);
        }
    }

    public void displayPreviousSet(View view)
    {
        if (mCurrentIndex >= mDisplayCount)
        {
            mCurrentIndex -= 20;
            mSpellingList = mSpellingBee.getSpellingArrayList(mCurrentIndex, mCurrentIndex + mDisplayCount);
            setTitle(getTitleCurrent());
            mListView.setAdapter(new PracticeAdapter(this, R.layout.list_item_practice, mSpellingList));
            mListView.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    mListView.setSelection(0);
                }
            }, 100L);
        }
    }

    private String getTitleCurrent()
    {
        String title;

        if (mSpellingList.size() == 0)
        {
            title = mSpellingBee.getTitle() + " - 0";
        }
        else if (mSpellingList.size() < mDisplayCount)
        {
            title = mSpellingBee.getTitle() + " - " + Integer.toString(mCurrentIndex + 1) + ":" + Integer.toString(mCurrentIndex + mSpellingList.size());
        }
        else
        {
            title = mSpellingBee.getTitle() + " - " + Integer.toString(mCurrentIndex + 1) + ":" + Integer.toString(mCurrentIndex + mDisplayCount);
        }

        title += " / " + Integer.toString(mSpellingBee.getSpellingArrayList().size());

        return title;
    }

    public void goHome(View view)
    {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.practice_activity);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        mListView = (ListView) findViewById(R.id.listViewSpellings);
        mSpellingBee = (SpellingBee) getApplication();

        mCurrentIndex = 0;

        mSpellingList = mSpellingBee.getSpellingArrayList(mCurrentIndex, mCurrentIndex + mDisplayCount);
        setTitle(getTitleCurrent());
        mListView.setAdapter(new PracticeAdapter(this, R.layout.list_item_practice, mSpellingList));
        mListView.setOnItemClickListener(mItemClickListener);
    }
    
    public void playContinously(View view)
    {
        for (int index = 0; index < mSpellingList.size(); index++)
        {
            mSpellingBee.getTextToSpeech().speak(mSpellingList.get(index).getSpelling(), TextToSpeech.QUEUE_FLUSH, null);
            SystemClock.sleep(5000);
        }
    }

}
